<?php

class Clix
{
    protected $view;
    protected $config;
    protected $uri;
    protected $params;
    protected $raw;
    protected $db;
    protected $actionTemplate;

    public function __construct($c = null, $run = true)
    {
        if (session_id()=='') {
            session_start();
        }
        $this->view = new ClixView();
        $this->view->setScriptsPath('./views');
        
        $this->config = array(
            'clix' => array(
                'base' => ''
            )
        );
        if ($c!=null) {
            $this->config = array_merge($this->config, parse_ini_file($c, true));
        } 
        if (empty($this->config['clix']['base'])) {
            $this->config['clix']['base'] = preg_replace("/\/index.php/i", '', $_SERVER['SCRIPT_NAME']);
        }
        if (isset($_GET['force-mobile'])) {
            $_SESSION['_frmb'] = ($_GET['force-mobile'] == 1); 
        }
        if ((preg_match("/(android)|(webOS)|(nokia)|(Windows CE)|(iphone)|(vodafone)|(PalmSource)|(J2ME)/i", $_SERVER['HTTP_USER_AGENT'])or($_SESSION['_frmb']))and(is_dir('./views/mobile'))) {
            $this->view->_m = true;
        }
        
        $this->view->config = $this->config['clix'];
                
        if (!empty($this->config['db'])) {
            $this->db = new ClixDb($this->config['db']);
        }
        
        if (!empty($this->config['clix']['layout'])) {
            if (($this->config['clix']['layout']=='1')or
                ($this->config['clix']['layout']=='true')) {
                $this->view->setLayout('layout.phtml');    
            } else {
                $this->view->setLayout($this->config['clix']['layout']);
            }
        } elseif ($this->view->exists('layout.phtml')) {
            $this->config['clix']['layout'] = 'layout.phtml';
            $this->view->setLayout('layout.phtml');
        }
        
        $urifilter = array(
            "/\?.*/",
            "/^" . preg_quote($this->config['clix']['base'], '/') . "/i", // remove base uri
            "/^\/+/", // remove first slashes 
            "/\/+$/" // remove last slashes
        );
        
        $this->uri = preg_replace($urifilter, '', $_SERVER['REQUEST_URI']); 
        
        $this->params = explode('/', $this->uri);
        if (empty($this->params[0])) {
            $this->params[0] = 'index';
        }
        $this->raw = $this->params;
        $this->params[0] = strtolower($this->params[0]);
        
        $this->init();
        
        if ($run) {
            $this->run();
        }
    }

    public function _getParam($i, $r = false)
    {
        $p = $this->params;
        if ($r) {
            $p = $this->raw; 
        }
        if (isset($p[$i])) {
            return $p[$i];
        }
        return false;
    }
    
    public function init() { }
    
    public function run()
    {
        $action = $this->camel("{$this->params[0]}Action");
        $view = strtolower(implode('/', $this->params) . '.phtml');
        if (method_exists($this, $action)) {
            $this->actionTemplate = $this->params[0] . '.phtml';
            $this->$action();
            echo $this->view->render($this->actionTemplate);
        } elseif ($this->view->exists($view)) {
            echo $this->view->render($view);
        } elseif (method_exists($this, 'defaultAction')) {
            $action = 'defaultAction';
            $this->actionTemplate = 'default.phtml';
            $this->$action();
            echo $this->view->render($this->actionTemplate);
        } else {
            $this->errorAction();
            echo $this->view->render('error.phtml');
        }
    }
    
    public function camel($t)
    {
        $r = '';
        for ($i = 0; $i < strlen($t); $i++) {
            if ($t[$i]=='-') {
                $i++;
                $r .= strtoupper($t[$i]);
            } else {
                $r .= $t[$i];
            }
        }
        return $r;
    }
    
    public function errorAction()
    {
        header("HTTP/1.0 404 Not Found");
    }
    
    public function _redirect($u)
    {
        if (!preg_match("/^http/i", $u)) {
            header("Location: {$this->config['clix']['base']}{$u}");
        } else {
            header("Location: {$u}");
        }
        exit;
    }
}
